package me.ingxin.android.permission.sample

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import me.ingxin.android.permission.sample.databinding.FragmentMainBinding
import me.ingxin.android.permission2.PERMISSION_POST_NOTIFICATIONS
import me.ingxin.android.permission2.PermissionX
import me.ingxin.android.permission2.strategy.ReasonStrategy

/**
 * 文件描述.
 *
 * @author ingxin
 * @date 2022/4/1
 */
class MainFragment : Fragment() {

    private lateinit var binding: FragmentMainBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentMainBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnRequest.setOnClickListener { test() }
    }

    private fun test() {
        val permissions = mutableListOf(
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.SYSTEM_ALERT_WINDOW,
            Manifest.permission.WRITE_SETTINGS,
            Manifest.permission.REQUEST_INSTALL_PACKAGES,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_CALENDAR,
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.BODY_SENSORS,
            Manifest.permission.SEND_SMS,
            PERMISSION_POST_NOTIFICATIONS
        )

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            permissions.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
            permissions.add(Manifest.permission.ACTIVITY_RECOGNITION)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            permissions.add(Manifest.permission.MANAGE_EXTERNAL_STORAGE)
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            permissions.add(Manifest.permission.BODY_SENSORS_BACKGROUND)
            permissions.add(Manifest.permission.READ_MEDIA_IMAGES)
        } else {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        }

        PermissionX.with(this).permissions(*permissions.toTypedArray())
            .reasonStrategy(ReasonStrategy.Before)
            .message { list ->
            "申请说明：\n正在申请一些权限\n$list"
        }.request { allGranted, _, deniedList ->
                //allGranted表示权限全部成功
                //grantedList已获取的权限
                //deniedList被拒绝的权限
                if (allGranted) {
                    Toast.makeText(
                        requireContext(), "All permissions are granted", Toast.LENGTH_SHORT
                    ).show()
                } else {
                    Log.d("TAG", "The following permissions are denied：$deniedList")
                    Toast.makeText(
                        requireContext(), "permissions are denied：$deniedList", Toast.LENGTH_SHORT
                    ).show()
                }
            }
    }


}